import * as THREE from "three";

const geometry = new THREE.BufferGeometry();
const point1 = new THREE.Vector3(0, 0, 0);
const point2 = new THREE.Vector3(300, 0, 0);
const point3 = new THREE.Vector3(0, 300, 0);
geometry.setFromPoints([point1, point2, point3]);
const material = new THREE.MeshBasicMaterial({
	color: 0x00ff00,
	side: THREE.DoubleSide,
});
const mesh = new THREE.Mesh(geometry, material);

const ray = new THREE.Ray();
ray.origin.set(50, 50, 500);
ray.direction = new THREE.Vector3(0, 0, -1);

const arrowHelper = new THREE.ArrowHelper(
	ray.direction,
	ray.origin,
	300,
	0xff0000
);
mesh.add(arrowHelper);

const point = new THREE.Vector3();
ray.intersectTriangle(point1, point2, point3, false, point);
console.log(point);

const meshGroup = new THREE.Group();
function generateBox(x, y, z, color) {
	const geometry = new THREE.BoxGeometry(100, 100, 100);
	const material = new THREE.MeshBasicMaterial({ color: color });
	const mesh = new THREE.Mesh(geometry, material);
	mesh.position.set(x, y, z);
	meshGroup.add(mesh);
}
const box1 = generateBox(200, 0, 0, 0x00ff00);
const box2 = generateBox(0, 200, 0, 0x0000ff);
const box3 = generateBox(0, 0, 200, 0xff0000);

// setTimeout(() => {
// 	// 计算射线与三角形的交点
// 	const raycaster = new THREE.Raycaster(ray.origin, ray.direction);

// 	// const arrowHelper = new THREE.ArrowHelper(ray.direction, ray.origin, 600);
// 	// meshGroup.add(arrowHelper);

// 	const intersects = raycaster.intersectObject(meshGroup);
// 	intersects.forEach((item) => {
// 		item.object.material.color = new THREE.Color("pink");
// 	});
// }, 1000);

export { mesh, meshGroup };
